//
//  ECLoginBindingController.m
//  Edu_iOS2018
//
//  Created by apple on 2018/2/27.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECLoginBindingController.h"
#import "ECUITextField_Account.h"
#import "UINavigationBar+Awesome.h"
#import "UIImage+Color.h"
#import "ECForgetPasswordViewController.h"
#import "CYDataHelper.h"

#import <ShareSDK/SSDKUser.h>

@interface ECLoginBindingController ()
///用户名
@property (nonatomic, strong) ECUITextField_Account *phoneArea;
///手机号
@property (nonatomic, strong) ECUITextField_Account *passwordArea;
///登录按钮
@property (nonatomic, strong) ECUButton *loginBtn;
@end

@implementation ECLoginBindingController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar lt_reset];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"navigation_Return"] style:UIBarButtonItemStylePlain target:self action:@selector(action_dismiss)];
    /*  导航栏字体  */
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#333333"]}];
    //
    /*  导航栏线  */
    [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"#BFBFBF"] size:CGSizeMake(self.view.frame.size.width, 0.5)]];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"登录绑定";
        
    self.phoneArea = [[ECUITextField_Account alloc]initWithFrame:CGRectMake(kProportion_Width(60), kProportion_Width(MarginHeight_textfield), kScreenWidth - kProportion_Width(60*2), (kScreenWidth - kProportion_Width(60*2))* 0.1406) placeholder:@"手机号/用户名/邮箱" leftImage:[UIImage imageNamed:@"tel_user"]];
    [self.view addSubview:self.phoneArea];
    
    self.passwordArea = [[ECUITextField_Account alloc]initWithFrame:CGRectMake(kProportion_Width(MarginLeft_Textfield), CGRectGetMaxY(self.phoneArea.frame) + kProportion_Width(MarginHeight_textfield), kScreenWidth - kProportion_Width(MarginLeft_Textfield *2), (kScreenWidth - kProportion_Width(60*2))* 0.1406) placeholder:@"密码" leftImage:[UIImage imageNamed:@"pwd_user"]];
    self.passwordArea.textField.frame = CGRectMake(self.passwordArea.textField.mj_x, self.passwordArea.textField.mj_y, kScreenWidth - kProportion_Width(MarginLeft_Textfield*2) - kProportion_Width(268) - 5, self.passwordArea.textField.mj_h);
    [self.passwordArea.rightButton setTitle:@"忘记密码?" forState:0];
    self.passwordArea.rightButton.titleLabel.font = [UIFont systemFontOfSize:14*kPercentage];
    [self.passwordArea.rightButton setTitleColor: [UIColor colorWithHexString:@"#313131"] forState:0];
    [self.passwordArea.rightButton setAdjustsImageWhenHighlighted:NO];
    self.passwordArea.rightButton.frame = CGRectMake(CGRectGetMaxX(self.passwordArea.textField.frame), 0, kProportion_Width(268), self.passwordArea.textField.mj_h);
    self.passwordArea.textField.secureTextEntry = YES;
    [self.view addSubview:self.passwordArea];
    WeakSelf(weakSelf);
    self.passwordArea.block_rightButton = ^(id obj) {
        //获取验证码处理
        [weakSelf block_forgetPassword];
    };
    
    
    // login btn
    self.loginBtn = [ECUButton buttonWithType:UIButtonTypeCustom];
    [self.loginBtn setBackgroundImage:[UIImage imageNamed:@"flatButton_colorNormal"] forState:UIControlStateNormal];
    self.loginBtn.frame = CGRectMake(kProportion_Width(60), CGRectGetMaxY(self.passwordArea.frame) + kProportion_Width(MarginHeight_textfield), kScreenWidth - kProportion_Width(60*2),(kScreenWidth - kProportion_Width(60*2))* 0.1406);
    [self.loginBtn addTarget:self action:@selector(action_Login) forControlEvents:UIControlEventTouchUpInside];
    [self.loginBtn setBackgroundImage:[UIImage imageNamed:@"flatButton_colorSelected"] forState:UIControlStateHighlighted];
    [self.loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    self.loginBtn.titleLabel.font = [UIFont systemFontOfSize:ceil(20)];
    [self.loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.loginBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    [self.view addSubview:self.loginBtn];
    
}

#pragma mark - 忘记密码 -
- (void)block_forgetPassword{
    [self.view endEditing:YES];
    ECForgetPasswordViewController *uivc_forgetpwd = [[ECForgetPasswordViewController alloc]init];
    uivc_forgetpwd.type_sendCode = @"sms_change_password"; 
    uivc_forgetpwd.type_checkNext = @"sms_change_password";
    [self.navigationController pushViewController:uivc_forgetpwd animated:YES];
    
}

-(void)action_Login{
    DLog(@"登录绑定按钮");
    
    if ([CYDataHelper isNull:self.phoneArea.textField.text]) {
        [MBProgressHUD showTipMessageInWindow:@"请输入手机号"];
        return;
    }
    
    if ([CYDataHelper isNull:self.passwordArea.textField.text]) {
        [MBProgressHUD showTipMessageInWindow:@"请输入密码"];
        return;
    }
    [HWHttpTool requestWithMethod:@"POST"
                              url:URL_POST_Login
                           params:@{
                                    @"nickname":self.phoneArea.textField.text,
                                    @"password":self.passwordArea.textField.text,
                                    @"type":self.platformType,
                                    @"id":self.unionid,
                                    }
                          headers:nil
                          success:^(id json) {
                              if ([json[@"code"] integerValue] == 200) {//返回成功
                                  // 保存token
                                  [UserInfoManager configInfo:@{@"token":json[@"data"][@"token"]}];//暂时只保存token
                                  [UserInfoManager configInfo:@{@"nickName":json[@"data"][@"user"][@"nickname"]}];//姓名
                                  
                                  NSString *imageUrl = json[@"data"][@"user"][@"smallAvatar"];
                                  [UserInfoManager configInfo:@{@"smallAvatar":imageUrl}];//头像
                                  
                                  NSString *sign = json[@"data"][@"user"][@"signature"];
                                  if ([CYDataHelper isNull:sign]) {//判断是否为空 (个签这个字段返回数据和其余不一样 @"<null>")
                                      sign = @"";
                                  }
                                  [UserInfoManager configInfo:@{@"signAture":sign}];//个签
                                  
                                  [self.view endEditing:YES];//收起键盘
                                  //发送登录通知
                                  NSNotification *notification =[NSNotification notificationWithName:kNotifactionUserLogin object:nil userInfo:nil];
                                  [[NSNotificationCenter defaultCenter] postNotification:notification];
                                  // 退出界面
                                  [self dismissViewControllerAnimated:YES completion:nil];
                              }else{
                                  [MBProgressHUD showTipMessageInWindow:json[@"message"]];
                              }
                          }
                          failure:^(NSError *error) {
                              NSString *str = [ECData_Json requestErrorTipWith:error];
                              [MBProgressHUD showTipMessageInWindow:str];
                          }];
}

-(void)action_dismiss{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
